home *** CD-ROM | disk | FTP | other *** search
- /* --------------------------------------------------------------
- Apple Worldwide Developer Technical Support
-
- MLTESampleShell.c
-
- You may incorporate this sample code into your applications without
- restriction, though the sample code has been provided "AS IS" and the
- responsibility for its operation is 100% yours. However, what you are
- not permitted to do is to redistribute the source as "Apple Sample Code"
- after having made changes. If you're going to re-distribute the source,
- we require that you make it clear in the source that the code was
- descended from Apple Sample Code, but that you've made changes.
-
- Copyright © 2000 Apple Computer, Inc., All Rights Reserved
-
- ----------------------------------------------------------------- */
- #include "MLTESampleShell.h"
-
- void main(void)
- {
- Initialize();
- EventLoop();
- }
-
- void Initialize()
- {
- Handle menuBar;
- OSStatus status = noErr;
-
- MaxApplZone();
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- FlushEvents(everyEvent,0);
- TEInit();
- InitDialogs(NULL);
- InitCursor();
- InstallAppleEventHandlers();
- CheckSystemVersion();
-
- // Step 1 -- Initialize MLTE
- TXNMacOSPreferredFontDescription defaults;
-
- defaults.fontID = kTXNDefaultFontName;
- defaults.pointSize = kTXNDefaultFontSize;
- defaults.fontStyle = kTXNDefaultFontStyle ;
- defaults.encoding = kTXNSystemDefaultEncoding;
- status = TXNInitTextension(&defaults, 1, 0);
- if (status != noErr)
- BigBadError(eNoMLTE);
-
- menuBar = GetNewMBar(rMenuBar); // read in menubar definition
- if (menuBar == NULL)
- BigBadError(eNoMemory);
- SetMenuBar(menuBar); // install menus
- DisposeHandle(menuBar);
- AppendResMenu(GetMenuHandle(mApple), 'DRVR'); // add Apple Menu Items
-
- // Step 27 -- Create the TXNFontMenuObject
- status = TXNNewFontMenuObject(GetMenuHandle(mFont), mFont, kStartHierMenuID, &gTXNFontMenuObject);
- if (status != noErr)
- BigBadError(eNoCreateFontMenuObject);
-
- DrawMenuBar();
-
- gInBackground = false;
- gNumDocuments = 0;
- DoNew(); // create a single empty document
- }
-
- // Check for Mac OS 8.5 or later
- static void CheckSystemVersion(void)
- {
- OSErr err;
- long response;
-
- err = Gestalt(gestaltSystemVersion, &response);
- if (response < 0x00000850)
- BigBadError(eWrongSystem);
- }
-
- // Get events -- handle them in DoEvent. Call AdjustCursor each time through the loop.
- void EventLoop()
- {
- RgnHandle cursorRgn;
- Boolean gotEvent;
- EventRecord event;
-
- cursorRgn = NewRgn(); // pass WNE an empty region the 1st time thru
-
- do {
- gotEvent = WaitNextEvent(everyEvent, &event, GetSleep(), cursorRgn);
-
- if (gotEvent)
- {
- // make sure we have the right cursor before handling the event
- AdjustCursor(cursorRgn);
- DoEvent(&event);
- }
- else
- {
- DoIdle(); // perform idle tasks when it’s not our event
- // Step 7 -- give time to TSM.
- TXNTSMCheck(NULL, &event);
- }
- } while (true); // loop forever... we quit via ExitToShell
- }
-
- // Determine what kind of event we have, and call the appropriate routines.
- void DoEvent(EventRecord *event)
- {
- short part, err;
- WindowPtr window;
-
- switch (event->what)
- {
- case nullEvent:
- DoIdle();
- break;
- case mouseDown:
- part = FindWindow(event->where, &window);
- switch (part)
- {
- case inMenuBar: // process a mouse menu command (if any)
- AdjustMenus(); // bring ’em up-to-date
- DoMenuCommand(MenuSelect(event->where));
- break;
- case inSysWindow: // let the system handle the mouseDown
- SystemClick(event, window);
- break;
- case inContent:
- if (window != FrontWindow())
- SelectWindow(window);
- else
- {
- DoContentClick(window, event);
- AdjustMenus();
- }
- break;
- case inDrag: // pass screenBits.bounds to get all gDevices
- DragWindow(window, event->where, &qd.screenBits.bounds);
- break;
- case inGoAway:
- if (TrackGoAway(window, event->where))
- DoCloseWindow(window); // we don’t care if the user cancelled
- break;
- case inGrow:
- DoGrowWindow(window, event);
- break;
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(window, event->where, part))
- DoZoomWindow(window, part);
- break;
- }
- break;
- case keyDown:
- case autoKey: // check for menukey equivalents
- if (event->modifiers & cmdKey) // Command key down
- {
- if (event->what == keyDown)
- {
- AdjustMenus(); // enable/disable/check menu items properly
- DoMenuCommand(MenuKey(event->message & charCodeMask));
- }
- }
- else
- DoKeyDown(event);
- break;
- case activateEvt:
- DoActivate((WindowPtr) event->message, (event->modifiers & activeFlag) != 0);
- break;
- case updateEvt:
- DoUpdate((WindowPtr) event->message);
- break;
- case diskEvt: // handle disk inserted events to recognize uninitialized floppies.
- if (HiWord(event->message) != noErr)
- {
- Point aPoint;
- SetPt(&aPoint, kDILeft, kDITop);
- err = DIBadMount(aPoint, event->message);
- }
- break;
- case kOSEvent:
- // BitAND with 0x0FF to get only low byte
- switch ((event->message >> 24) & 0x0FF) // high byte of message
- {
- case mouseMovedMessage:
- DoIdle(); // mouse-moved is also an idle event
- break;
- case suspendResumeMessage: // suspend/resume is also an activate/deactivate
- gInBackground = (event->message & kResumeMask) == 0;
- DoActivate(FrontWindow(), !gInBackground);
- break;
- }
- break;
- }
- }
-
- // Change the cursor's shape, depending on its position.
- void AdjustCursor(RgnHandle region)
- {
- WindowPtr window = FrontWindow(); // only adjust when we are in front
- TXNObject object = NULL;
-
- // Step 8 -- let MLTE adjust the cursor.
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNAdjustCursor(object, region);
- }
- }
-
- // Handle a mouseDown in the grow box of an active window.
- void DoGrowWindow(WindowPtr window, EventRecord *event)
- {
- TXNObject object = NULL;
-
- // Step 11 -- let MLTE grow the window.
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNGrowWindow(object, event);
- }
- }
-
- // Handle a mouseClick in the zoom box of an active window.
- void DoZoomWindow(WindowPtr window, short part)
- {
- TXNObject object = NULL;
-
- // Step 12 -- let MLTE zoom the window.
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNZoomWindow(object, part);
- }
- }
-
- // Called when an update event is received for a window.
- void DoUpdate(WindowPtr window)
- {
- GrafPtr curPort;
- TXNObject object = NULL;
-
- GetPort(&curPort);
- SetPort(window);
-
- // Step 14 -- let MLTE update the window.
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNUpdate(object); // TXNUpdate also calls BeginUpdate() and EndUpdate()
- }
- SetPort(curPort);
- }
-
- // Called when a window is activated or deactivated
- void DoActivate(WindowPtr window, Boolean becomingActive)
- {
- TXNObject object = NULL;
- TXNFrameID frameID = 0;
-
- // Step 15 -- let MLTE Activate and Focus the TNXObject.
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tFrm', sizeof(TXNFrameID), NULL, &frameID);
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
-
- if (becomingActive)
- {
- TXNActivate(object, frameID, becomingActive);
- AdjustMenus();
- }
- else
- TXNActivate(object, frameID, becomingActive);
- }
- TXNFocus(object, becomingActive);
- }
-
- // Called when a mouseDown occurs in the content of a window
- void DoContentClick(WindowPtr window, EventRecord *event)
- {
- TXNObject object = NULL;
-
- // Step 10 -- let MLTE handle the mouse down.
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNClick(object, event);
- }
- }
-
- // Called for any keyDown or autoKey events, except when the Command key is held down.
- void DoKeyDown(EventRecord *event)
- {
- WindowPtr window = FrontWindow();
- TXNObject object = NULL;
-
- // Step 13 -- let MLTE handle the key down
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNKeyDown(object, event);
- }
- }
-
- // Calculate sleep value for WaitNextEvent.
- static UInt32 GetSleep()
- {
- UInt32 sleep;
- WindowPtr window;
- TXNObject object = NULL;
-
- window = FrontWindow();
-
- // Step 6 -- let MLTE calc the appropriate sleep time.
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- sleep = TXNGetSleepTicks(object); // get appropriate sleep time
- }
- else
- sleep = GetCaretTime(); // use some other value
-
- return sleep;
- }
-
- // Called when we get a null event.
- void DoIdle()
- {
- WindowPtr window = FrontWindow();
- TXNObject object = NULL;
-
- // Step 9 -- let MLTE handle idle time.
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNIdle(object);
- }
- }
-
- // Enable and disable menus based on the current state.
- void AdjustMenus()
- {
- WindowPtr window;
- TXNObject object = NULL;
- MenuHandle menu;
- Boolean undo, redo, cutCopyClear, paste, selectAll;
-
- window = FrontWindow();
-
- menu = GetMenuHandle(mFile);
- if (gNumDocuments < kMaxOpenDocuments)
- EnableItem(menu, iNew); // New is enabled when we can open more documents
- else
- DisableItem(menu, iNew);
-
- if (window != NULL) // Printing and Close are enabled when a window is up
- {
- EnableItem(menu, iPageSetup);
- EnableItem(menu, iPrint);
- EnableItem(menu, iClose);
- }
- else
- {
- DisableItem(menu, iPageSetup);
- DisableItem(menu, iPrint);
- DisableItem(menu, iClose);
- }
-
- menu = GetMenuHandle(mEdit);
- undo = redo = cutCopyClear = paste = selectAll = false;
-
- if (IsDAWindow(window))
- undo = redo = cutCopyClear = paste = selectAll = true;
- else if (IsAppWindow(window))
- {
- // Step 16 -- decide if Undo and Redo should be enabled.
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- if (TXNCanUndo(object, NULL))
- undo = true;
- if (TXNCanRedo(object, NULL))
- redo = true;
- // Step 19 -- decide if Cut, Copy, Clear should be enabled.
- if (!TXNIsSelectionEmpty(object))
- cutCopyClear = true;
- // Step 23 -- decide if Paste should be enabled.
- if (TXNIsScrapPastable())
- paste = true;
- // Step 25 -- decide if Select All should be enabled.
- if (TXNDataSize(object))
- selectAll = true;
- }
-
- if (undo)
- EnableItem(menu, iUndo);
- else
- DisableItem(menu, iUndo);
-
- if (redo)
- EnableItem(menu, iRedo);
- else
- DisableItem(menu, iRedo);
-
- if (cutCopyClear)
- {
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iClear);
- }
- else
- {
- DisableItem(menu, iCut);
- DisableItem(menu, iCopy);
- DisableItem(menu, iClear);
- }
-
- if (paste)
- EnableItem(menu, iPaste);
- else
- DisableItem(menu, iPaste);
-
- if (selectAll)
- EnableItem(menu, iSelectAll);
- else
- DisableItem(menu, iSelectAll);
-
- InvalMenuBar(); // invalidate menubar - will update next event loop
- }
-
- // Handles items chosen from the menu bar.
- void DoMenuCommand(long menuResult)
- {
- WindowPtr window;
- TXNObject object = NULL;
- short menuID, menuItem;
- short itemHit, daRefNum;
- Str255 daName;
- OSStatus status = noErr;
-
- window = FrontWindow();
- menuID = HiWord(menuResult); // use macros to get menu
- menuItem = LoWord(menuResult); // item number and menu number
-
- switch (menuID)
- {
- case mApple:
- switch (menuItem)
- {
- case iAbout: // bring up alert for About
- itemHit = Alert(rAboutAlert, NULL);
-
- default: // non-About items are DAs et al
- MenuHandle appleMenuHandle = GetMenuHandle(mApple);
- if (appleMenuHandle != NULL)
- GetMenuItemText(appleMenuHandle, menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
- case mFile:
- switch (menuItem)
- {
- case iNew:
- DoNew();
- break;
- case iClose:
- DoCloseWindow(FrontWindow()); // ignore the result
- break;
- case iPageSetup:
- // Step 34 -- let MLTE handle Page Setup.
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNPageSetup(object);
- break;
- case iPrint:
- // Step 35 -- let MLTE handle Print.
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNPrint(object);
- break;
- case iQuit:
- Terminate();
- break;
- }
- break;
- case mEdit:
- if (IsAppWindow(window))
- {
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- switch (menuItem)
- {
- case iUndo:
- // Step 17 -- let MLTE handle Undo.
- TXNUndo(object);
- break;
- case iRedo:
- // Step 18 -- let MLTE handle Redo.
- TXNRedo(object);
- break;
- case iCut:
- // Step 20 -- let MLTE handle Cut.
- TXNCut(object);
- break;
- case iCopy:
- // Step 21 -- let MLTE handle Copy.
- TXNCopy(object);
- break;
- case iPaste:
- // Step 24 -- let MLTE handle Paste.
- TXNPaste(object);
- break;
- case iClear:
- // Step 22 -- let MLTE handle Clear.
- TXNClear(object);
- break;
- case iSelectAll:
- // Step 26 -- let MLTE handle Select All.
- TXNSelectAll(object);
- break;
- default:
- break;
- }
- }
- case mFont:
- DoFontMenu(menuID, menuItem, window);
- break;
- case mSize:
- if (IsAppWindow(window))
- {
- // Step 30 -- let MLTE change selected text to selected size.
- // The structure containing attributes we would like to set.
- TXNTypeAttributes typeAttr;
-
- // These are the possible size choices the app offers.
- static short aFontSizeList[] = {9, 10, 12, 14, 18, 24, 36};
-
- // This is the size the user chose, off 1 b/c arrays are zero-based
- short shortValue = aFontSizeList[menuItem - 1];
-
- // Constant specifying size attribute -- FOUR_CHAR_CODE('size')
- typeAttr.tag = kTXNQDFontSizeAttribute;
- // Constant specifying the size of the size attribute -- sizeof(SInt16)
- typeAttr.size = kTXNQDFontSizeAttributeSize;
- // move data into high byte
- typeAttr.data.dataValue = shortValue << 16;
-
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- // Set the size attributes
- TXNSetTypeAttributes(object, 1, &typeAttr, kTXNUseCurrentSelection, kTXNUseCurrentSelection);
- }
- break;
- case mStyle:
- if (IsAppWindow(window))
- {
- TXNTypeAttributes typeAttr; // struct of attributes we would like to set.
- Style newStyle;
-
- switch (menuItem)
- {
- case iPlain:
- newStyle = normal; // 0
- break;
- case iBold:
- newStyle = bold; // 1
- break;
- case iItalic:
- newStyle = italic; // 2
- break;
- case iUnderline:
- newStyle = underline; // 3
- break;
- case iOutline:
- newStyle = outline; // 4
- break;
- case iShadow:
- newStyle = shadow; // 0x10
- break;
- case iCondensed:
- newStyle = condense; // 0x20
- break;
- case iExtended:
- newStyle = extend; // 0x40
- break;
- default:
- break;
- }
- // Step 31 -- let MLTE change selected text to selected style.
- // Specify the style attributes
- typeAttr.tag = kTXNQDFontStyleAttribute;
- typeAttr.size = kTXNQDFontStyleAttributeSize;
- typeAttr.data.dataValue = newStyle;
-
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- // Set the style attributes
- TXNSetTypeAttributes(object, 1, &typeAttr, kTXNUseCurrentSelection, kTXNUseCurrentSelection);
- }
- break;
- case mLayout:
- if (menuItem <= iForceJustify)
- DoJustification(window, menuItem);
- else
- DoWordWrap(window);
- break;
- default:
- if (menuID >= kStartHierMenuID)
- DoFontMenu(menuID, menuItem, window);
- break;
- }
- HiliteMenu(0); // unhighlight the menu
- AdjustMenus();
- }
-
- // Handle font menu selections
- void DoFontMenu(short menuID, short menuItem, WindowPtr window)
- {
- TXNObject object = NULL;
- OSStatus status = noErr;
-
- if (IsAppWindow(window))
- {
- // Step 29 -- let MLTE change selected text to selected font.
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- if (gTXNFontMenuObject != NULL)
- status = TXNDoFontMenuSelection(object, gTXNFontMenuObject, menuID, menuItem);
- if (status != noErr)
- AlertUser(eNoFontName);
- }
- }
-
- // Set the justification tag
- void DoJustification(WindowPtr window, short menuItem)
- {
- TXNObject object = NULL;
- MenuHandle layoutMenu;
- OSStatus status = noErr;
-
- layoutMenu = GetMenuHandle(mLayout);
-
- if (IsAppWindow(window))
- {
- SInt32 justification;
- TXNControlTag controlTag[1];
- TXNControlData controlData[1];
-
- for (int i = 1; i <= iForceJustify; i++) // brute technique to uncheck last item
- CheckItem(layoutMenu, i, false);
-
- switch (menuItem)
- {
- case iDefaultJustify:
- justification = kTXNFlushDefault; // flush according to line direction
- break;
- case iLeftJustify:
- justification = kTXNFlushLeft;
- break;
- case iRightJustify:
- justification = kTXNFlushRight;
- break;
- case iCenterJustify:
- justification = kTXNCenter;
- break;
- case iFullJustify:
- justification = kTXNFullJust;
- break;
- case iForceJustify:
- justification = kTXNForceFullJust; // flush left for all scripts
- break;
- default:
- break;
- }
- CheckItem(layoutMenu, menuItem, true); // check this menu item
-
- // Step 33 -- let MLTE et the document's text justification.
- controlTag[0] = kTXNJustificationTag;
-
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- status = TXNGetTXNObjectControls(object, 1, controlTag, controlData);
-
- if (controlData[0].sValue != justification) // if we have a new justification
- {
- controlData[0].sValue = justification;
- status = TXNSetTXNObjectControls(object, false, 1, controlTag, controlData);
- }
- }
- }
-
- // Toggle word wrap
- void DoWordWrap(WindowPtr window)
- {
- TXNObject object = NULL;
- MenuHandle layoutMenu;
- OSStatus status = noErr;
-
- layoutMenu = GetMenuHandle(mLayout);
-
- if (IsAppWindow(window))
- {
- TXNControlTag controlTag[1];
- TXNControlData controlData[1];
-
- // Step 32 -- let MLTE set the document's word wrap state.
- controlTag[0] = kTXNWordWrapStateTag;
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- status = TXNGetTXNObjectControls(object, 1, controlTag, controlData);
-
- if (controlData[0].uValue == kTXNAutoWrap) // if we are autowrapped
- {
- controlData[0].uValue = kTXNNoAutoWrap; // toggle to not autowrapped
- CheckItem(layoutMenu, iAutoWrap, false); // uncheck this menu item
- }
- else
- {
- controlData[0].uValue = kTXNAutoWrap;
- CheckItem(layoutMenu, iAutoWrap, true); // check this menu item
- }
- status = TXNSetTXNObjectControls(object, false, 1, controlTag, controlData);
-
- if (status != noErr)
- AlertUser(eNoWordWrap);
- }
- }
-
- // Create a new document and window.
- void DoNew(void)
- {
- WindowPtr window;
- MenuHandle layoutMenu;
- OSStatus status = noErr;
-
- window = GetNewCWindow(rDocWindow, NULL, (WindowPtr)-1L);
-
- if (window != NULL)
- {
- // Step 3 -- create a new TXNObject.
- TXNObject object = NULL;
- TXNFrameID frameID = 0;
- Rect *framePtr = &window->portRect;
- TXNFrameOptions frameOptions = kTXNWantHScrollBarMask | kTXNWantVScrollBarMask
- | kTXNDrawGrowIconMask | kTXNShowWindowMask |kTXNNoKeyboardSyncMask;
-
- status = TXNNewObject( NULL, // start with an empty document
- window,
- framePtr, // the area to fill
- frameOptions,
- kTXNTextEditStyleFrameType,
- kTXNTextensionFile,
- kTXNSystemDefaultEncoding,
- &object, // ptr to opaque TXNObject
- &frameID, // always 0 in ver 1.0
- 0);
- if (status == noErr)
- {
- // Step 4 -- store a reference to the oject and the frameID
- SetWindowProperty(window,'GRIT','tFrm',sizeof(TXNFrameID),&frameID);
- SetWindowProperty(window,'GRIT','tObj',sizeof(TXNObject),&object);
- AdjustMenus();
- gNumDocuments++; // will be decremented in DoCloseWindow
- DoJustification(window, iDefaultJustify); // set justification default for new doc
- layoutMenu = GetMenuHandle(mLayout);
- CheckItem(layoutMenu, iAutoWrap, true); // check this menu item
- // ShowWindow(window);
- }
- else
- {
- CloseWindow(window);
- window = NULL;
- AlertUser(eNoWindow); // TXNNewObject failed - tell user
- }
- }
- else
- AlertUser(eNoWindow); // window was null - tell user
- }
-
- // Close a window. Handles desk accessory and application windows.
- Boolean DoCloseWindow(WindowPtr window)
- {
- TXNObject object = NULL;
- Boolean isClosed = true;
-
- if (IsDAWindow(window))
- {
- CloseDeskAcc(GetWindowKind(window));
- }
- else if (IsAppWindow(window))
- {
-
- if (isClosed)
- {
- // Step 5 -- delete the TXNObject.
- GetWindowProperty(window, 'GRIT', 'tObj', sizeof(TXNObject), NULL, &object);
- TXNDeleteObject(object);
-
- CloseWindow(window);
- DisposePtr((Ptr) window);
- gNumDocuments -= 1;
- }
- }
- AdjustMenus();
- return isClosed;
- }
-
- // Clean up and exit. Close all of the windows.
- void Terminate()
- {
- WindowPtr aWindow;
- Boolean closed;
- OSStatus status = noErr;
-
- closed = true;
- do {
- aWindow = FrontWindow(); // get current front window
- if (aWindow != NULL)
- closed = DoCloseWindow(aWindow); // close this window
- }
- while (closed && (aWindow != NULL));
-
- if (closed)
- {
- // Step 28 -- dispose font menu object before terminating Textension.
- if (gTXNFontMenuObject != NULL)
- {
- status = TXNDisposeFontMenuObject(gTXNFontMenuObject);
- if (status != noErr)
- AlertUser(eNoDisposeFontMenuObject);
- gTXNFontMenuObject = NULL; // Nullify font menu object.
- }
-
- // Step 2 -- Terminate MLTE
- TXNTerminateTextension();
- ExitToShell();
- }
- }
-
- // Check whether a window is a document window created by the application.
- // These windows are distinguished from desk accessories, dialogs, and
- // other windows by their windowKind userKind.
- Boolean IsAppWindow(WindowPtr window)
- {
- OSErr status = noErr;
-
- return (window != NULL) && (((WindowPeek) window)->windowKind == userKind);
- }
-
- // Check to see if a window belongs to a desk accessory.
- Boolean IsDAWindow(WindowPtr window)
- {
- if (window == NULL)
- return false;
- else // DA windows have negative windowKinds
- return ((WindowPeek) window)->windowKind < 0;
- }
-
- // Display an alert that tells the user an error occurred.
- void AlertUser(short error)
- {
- short itemHit;
- Str255 message;
-
- SetCursor(&qd.arrow);
- GetIndString(message, kErrStrings, error);
- ParamText(message, (ConstStr255Param)"",(ConstStr255Param)"", (ConstStr255Param)"");
- itemHit = Alert(rUserAlert, NULL);
- }
-
- // Used whenever a fatal error happens
- void BigBadError(short error)
- {
- AlertUser(error);
- ExitToShell();
- }
-
- // install the core apple event handlers
- void InstallAppleEventHandlers(void)
- {
- long result;
-
- OSErr err = Gestalt(gestaltAppleEventsAttr, &result);
-
- if (err == noErr)
- { // we should check the AEInstallEventHandler return value but since it's just a sample...
- AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(HandleOapp), 0, false);
- AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(HandleOdoc), 0, false);
- AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(HandlePdoc), 0, false);
- AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(HandleQuit), 0, false);
- }
- }
-
- // Respond to an open application apple event
- pascal OSErr HandleOapp (const AppleEvent *aevt, AEDescList *reply, UInt32 refCon)
- {
- #pragma unused (aevt, reply, refCon)
- return noErr;
- }
-
- // Respond to an open document apple event
- pascal OSErr HandleOdoc (const AppleEvent *aevt, AEDescList *reply, UInt32 refCon)
- {
- #pragma unused (aevt, reply, refCon)
- return errAEEventNotHandled;
- }
-
- // Respond to a print apple event
- pascal OSErr HandlePdoc (const AppleEvent *aevt, AEDescList *reply, UInt32 refCon)
- {
- #pragma unused (aevt, reply, refCon)
- return errAEEventNotHandled;
- }
-
- // Respond to a quit apple event
- pascal OSErr HandleQuit (const AppleEvent *aevt, AEDescList *reply, UInt32 refCon)
- {
- #pragma unused (aevt, reply, refCon)
- Terminate();
- return noErr;
- }